
package com.gemframework.modules.prekit.flow.controller;

import com.gemframework.common.constant.GemModules;
import com.gemframework.model.common.BaseResultData;
import com.gemframework.model.common.PageInfo;
import com.gemframework.model.enums.FlowResType;
import com.gemframework.modules.prekit.flow.entity.GemProcdef;
import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.DynamicBpmnService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * @Title: ProcdefController
 * @Package: com.gemframework.modules.perkit.demo
 * @Date: 2020-04-16 13:36:13
 * @Version: v1.0
 * @Description: 流程定义控制器
 * @Author: nine QQ 769990999
 * @Copyright: Copyright (c) 2020 wanyong
 * @Company: www.gemframework.com
 */
@Slf4j
@RestController
@RequestMapping(GemModules.PreKit.PATH_FLOW+"/procdef")
public class ProcdefController {

    @Autowired
    RepositoryService repositoryService;
    @Autowired
    DynamicBpmnService dynamicBpmnService;
    @Resource
    private HttpServletResponse httpServletResponse;


    @GetMapping("/page")
    public BaseResultData page(PageInfo pageInfo,String name) {
        List<ProcessDefinition> processDefinitions;
        long count;
        if(StringUtils.isNotBlank(name)) {
            processDefinitions = repositoryService.createProcessDefinitionQuery()
                    .processDefinitionNameLike(name).orderByProcessDefinitionId().desc()
                    .listPage(pageInfo.getLimit().intValue() * (pageInfo.getPage().intValue() - 1), pageInfo.getLimit().intValue());
            count = repositoryService.createProcessDefinitionQuery().processDefinitionNameLike(name).count();
        }else{
            processDefinitions = repositoryService.createProcessDefinitionQuery()
                    .orderByProcessDefinitionId().desc()
                    .listPage(pageInfo.getLimit().intValue() * (pageInfo.getPage().intValue() - 1), pageInfo.getLimit().intValue());
            count = repositoryService.createProcessDefinitionQuery().count();
        }
        List<GemProcdef> list = new ArrayList<>();
        for(ProcessDefinition processDefinition: processDefinitions){
            Deployment deployment = repositoryService.createDeploymentQuery().deploymentId(processDefinition.getDeploymentId()).singleResult();
            GemProcdef procdef = new GemProcdef(processDefinition);
            procdef.setName(deployment.getName());
            procdef.setCategory(deployment.getCategory());
            list.add(procdef);
        }
        return BaseResultData.SUCCESS(list,count);
    }

    @GetMapping("/viewResource")
    public void viewResource(String type,String deploymentId) {
        List<String> names = repositoryService.getDeploymentResourceNames(deploymentId);
        String resourceName = "";
        for (String name : names) {
            if(type.equals(FlowResType.IMG.getCode())){
                if(name.indexOf(".png")>=0){
                    resourceName = name;
                }
            }
            if(type.equals(FlowResType.BPMN.getCode())){
                if(name.indexOf(".xml")>=0){
                    resourceName = name;
                }
            }

        }
        InputStream in = repositoryService.getResourceAsStream(deploymentId, resourceName);
        try {
            OutputStream out = httpServletResponse.getOutputStream();
            // 把图片的输入流程写入resp的输出流中
            byte[] b = new byte[1024];
            for (int len = -1; (len= in.read(b))!=-1; ) {
                out.write(b, 0, len);
            }
            // 关闭流
            out.close();
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    @GetMapping("/info")
    public BaseResultData info(String id) {
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().processDefinitionId(id).singleResult();
        return BaseResultData.SUCCESS(processDefinition);
    }

    /**
     * 挂起
     * @param id
     * @return
     */
    @PostMapping("/suspend")
    public BaseResultData suspend(String id) {
        repositoryService.suspendProcessDefinitionById(id);
        return BaseResultData.SUCCESS();
    }

    /**
     * 激活
     * @param id
     * @return
     */
    @PostMapping("/activate")
    public BaseResultData activate(String id) {
        repositoryService.activateProcessDefinitionById(id);
        return BaseResultData.SUCCESS();
    }
}
